package com.ruoyi.web.controller.statistic.admin;

import com.ruoyi.common.constant.ConfigConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.statistic.domain.StatisticSelectListVo;
import com.ruoyi.statistic.domain.StatisticStudentSchoolInfo;
import com.ruoyi.statistic.service.IStatisticStudentSchoolInfoService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


@RestController
@RequestMapping("/admin/employment/statistic/schoolInfo")
public class StatisticSchoolInfoController extends BaseController {
    @Resource
    private IStatisticStudentSchoolInfoService statisticStudentSchoolInfoService;

    @Resource
    private ISysDictTypeService dictTypeService;


    @Resource
    private ISysConfigService sysConfigService;

    /**
     * 初始化年级、系别、专业
     *
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('system:school:init')")
    @GetMapping(value = "/init")
    public AjaxResult init() {
        //清空原有表数据
        statisticStudentSchoolInfoService.deleteAll();
        //获取年级、系别、专业聚合
        List<StatisticStudentSchoolInfo> query = statisticStudentSchoolInfoService.aggregationStatisticStudentSchoolInfo();
        for (StatisticStudentSchoolInfo info : query) {
            statisticStudentSchoolInfoService.insertStatisticStudentSchoolInfo(info);
        }
        return success("初始化成功！");
    }


    /**
     * 获取校区selectlist列表
     *
     * @return
     */
//    @GetMapping(value = "/campus/selectlist")
//    public AjaxResult campusSelectList() {
//        List<SysDictData> campusList = dictTypeService.selectDictDataByType("campus");
//        List<SelectListVo> list = new ArrayList<>();
//        for (SysDictData data : campusList) {
//            SelectListVo dto = new SelectListVo();
//            dto.setLabel(data.getDictLabel());
//            dto.setValue(data.getDictLabel());
//            list.add(dto);
//        }
//        return success(list);
//    }


    /**
     * 获取年级selectlist列表
     *
     * @return
     */
    @GetMapping(value = "/grade/selectlist")
    public AjaxResult gradeSelectList() {
        List<StatisticStudentSchoolInfo> query = statisticStudentSchoolInfoService.selectStatisticAggregationGrade();
        List<StatisticSelectListVo> list = new ArrayList<>();
        for (StatisticStudentSchoolInfo data : query) {
            StatisticSelectListVo dto = new StatisticSelectListVo();
            dto.setLabel(data.getGrade());
            dto.setValue(data.getGrade());
            list.add(dto);
        }
        return success(list);
    }


    /**
     * 获取系别列表
     *
     * @return
     */
//    @GetMapping(value = "/department/list")
//    public AjaxResult getDepartmentList() {
//        List<DepartmentListVo> query = schoolGradeInfoService.selectDepartmentGroup();
//        List<SelectListVo> list = new ArrayList<>();
//        for (DepartmentListVo vo : query) {
//            SelectListVo dto = new SelectListVo();
//            dto.setLabel(vo.getDepartment());
//            dto.setValue(vo.getDepartment());
//            list.add(dto);
//        }
//        return success(list);
//    }


    /**
     * 获取院系selectlist列表
     *
     * @return
     */
    @GetMapping(value = "/department/selectlist")
    public AjaxResult departmentSelectList() {
        List<StatisticStudentSchoolInfo> query = statisticStudentSchoolInfoService.selectStatisticAggregationDepartment();
        List<StatisticSelectListVo> list = new ArrayList<>();
        for (StatisticStudentSchoolInfo data : query) {
            StatisticSelectListVo dto = new StatisticSelectListVo();
            dto.setLabel(data.getDepartment());
            dto.setValue(data.getDepartment());
            list.add(dto);
        }
        return success(list);
    }

    /**
     * 获取专业列表
     *
     * @return
     */
    @GetMapping(value = "/major/selectlist")
    public AjaxResult majorSelectList() {
        List<StatisticStudentSchoolInfo> query = statisticStudentSchoolInfoService.selectStatisticAggregationMajor();
        List<StatisticSelectListVo> list = new ArrayList<>();
        for (StatisticStudentSchoolInfo vo : query) {
            StatisticSelectListVo dto = new StatisticSelectListVo();
            dto.setLabel(vo.getMajor());
            dto.setValue(vo.getMajor());
            dto.setExt(vo.getDepartment());
            list.add(dto);
        }
        return success(list);
    }


    /**
     * 获取学历selectlist列表
     *
     * @return
     */
    @GetMapping(value = "/study/selectlist")
    public AjaxResult studySelectList() {
        List<SysDictData> studyList = dictTypeService.selectDictDataByType("education");
        List<StatisticSelectListVo> list = new ArrayList<>();
        for (SysDictData data : studyList) {
            StatisticSelectListVo dto = new StatisticSelectListVo();
            dto.setLabel(data.getDictLabel());
            dto.setValue(data.getDictLabel());
            list.add(dto);
        }
        return success(list);
    }

    @GetMapping(value = "/graduationYear/selectlist")
    public AjaxResult graduationYearSelectList() {
        String listStr = sysConfigService.selectConfigByKey(ConfigConstants.GRADUATION_YEAR_LIST);
        List<StatisticSelectListVo> list = new ArrayList<>();
        if (!StringUtils.isBlank(listStr)) {
            for (String item : listStr.split(",")) {
                StatisticSelectListVo dto = new StatisticSelectListVo();
                dto.setLabel(item);
                dto.setValue(item);
                list.add(dto);
            }
        }
        return success(list);
    }

    /**
     * 获取当前指定年份
     *
     * @return
     */
    @GetMapping(value = "/current/year")
    public AjaxResult currentYear() {
        String year = sysConfigService.selectConfigByKey(ConfigConstants.CURRENT_GRADUATION_YEAR);
        if (StringUtils.isEmpty(year)) {
            Calendar date = Calendar.getInstance();
            year = String.valueOf(date.get(Calendar.YEAR));
        }
        return AjaxResult.success("", year);
    }
}
